package product;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class cleantxt {

	/**清洗字符串
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		if (args.length!=2) {
			System.err.println("path err!");
			System.exit(-1);
		}
		@SuppressWarnings("deprecation")
		Job job=new Job(new Configuration(), "clean");
		job.setJarByClass(cleantxt.class);
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		job.setMapperClass(cleanmap.class);
		job.setReducerClass(cleanreduce.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(NullWritable.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(NullWritable.class);
		job.waitForCompletion(true);
	}
	public static class cleanmap extends Mapper<LongWritable, Text, Text, NullWritable>{
		protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,Text,NullWritable>.Context context) throws java.io.IOException ,InterruptedException {
			String[] lines = value.toString().split("，");
			for (int i = 0; i < lines.length; i++) {
				if (lines[i].contains("省")) {
					String trim = lines[i].trim();
					String provice=trim.substring(0,trim.indexOf("省"));
					context.write(new Text(provice), NullWritable.get());
				}else if(lines[i].contains("自治区")){
					String trim = lines[i].trim();
					String provice=trim.substring(0,trim.indexOf("自治区"));
					if (provice.length()==4) {
						context.write(new Text(provice.substring(0,2)), NullWritable.get());
					}else if(provice.length()==3 || provice.length()==2) {
						context.write(new Text(provice), NullWritable.get());
					}else {
						context.write(new Text(provice.substring(0,2)), NullWritable.get());
					}
					
				}else if(lines[i].contains("特别行政区")){
					String trim = lines[i].trim();
					String provice=trim.substring(0,trim.indexOf("特别行政区"));
					context.write(new Text(provice), NullWritable.get());
				}
			}
		};
	}
	public static class cleanreduce extends Reducer<Text, NullWritable,Text,NullWritable>{
		protected void reduce(Text k2, java.lang.Iterable<NullWritable>values, org.apache.hadoop.mapreduce.Reducer<Text,NullWritable,Text,NullWritable>.Context context) throws java.io.IOException ,InterruptedException {
			context.write(k2, NullWritable.get());
		};
	}

}
